/*
 * Copyright (c) 2020, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "bsp_tick.h"
#include "cmd_queue.h"
#include "ti_msp_dl_config.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

volatile uint8_t gEchoData = 0;

void sendString(const char *str) {
  while (*str != '\0') {
    DL_UART_Main_transmitDataBlocking(UART_0_INST, (uint8_t)*str);
    str++;
  }
}

void UART_printf(const char *format, ...) {
  uint32_t length;
  va_list args;
  uint32_t i;
  char TxBuffer[128] = {0};

  va_start(args, format);
  length = vsnprintf((char *)TxBuffer, sizeof(TxBuffer), (char *)format, args);
  va_end(args);

  for (i = 0; i < length; i++) {
    DL_UART_Main_transmitDataBlocking(UART_0_INST, TxBuffer[i]);
  }
}

QueueHandle q1;
const qdata my_header[] = {0x3F, 0x06};
qdata buffer[10];

int main(void) {
  SYSCFG_DL_init();
  systick_init(32000);

  NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);
  NVIC_EnableIRQ(UART_0_INST_INT_IRQN);

  // DL_SYSCTL_enableSleepOnExit();
  delay_ms(100);
  UART_printf("helloWorld\r\n");

  queue_init(&q1);

  while (1) {

    qsize len =
        queue_find_cmd_ext(&q1, buffer, sizeof(buffer), my_header, 2, 8);
    if (len > 0) {
      printf("Received command: cmd[2]=%d,cmd[3]=%d\r\n", buffer[2], buffer[3]);
    }
    delay_ms(30);
  }
}

void UART_0_INST_IRQHandler(void) {
  switch (DL_UART_Main_getPendingInterrupt(UART_0_INST)) {
  case DL_UART_MAIN_IIDX_RX:
    DL_GPIO_togglePins(GPIO_LEDS_PORT, GPIO_LEDS_USER_LED_1_PIN);
    while (!DL_UART_Main_isRXFIFOEmpty(UART_0_INST)) {
      gEchoData = DL_UART_Main_receiveData(UART_0_INST);
      queue_push(&q1, gEchoData);
    }
    break;
  default:
    break;
  }
}

int fputc(int _c, FILE *_fp) {
  DL_UART_Main_transmitDataBlocking(UART_0_INST, _c);
  return _c;
}

int fputs(const char *restrict s, FILE *restrict stream) {
  uint16_t i, len;
  len = strlen(s);
  for (i = 0; i < len; i++) {
    DL_UART_Main_transmitDataBlocking(UART_0_INST, s[i]);
  }
  return len;
}

int puts(const char *_ptr) {
  int cnt = fputs(_ptr, stdout);
  cnt += fputs("\n", stdout);
  return cnt;
}
